package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import MessagePacks.MessagePack;
import MessagePacks.UpdatePack;
import MessagePacks.checkAllMemReadEstReport;
import MessagePacks.getEstimationReport;
import MessagePacks.removeEsReport;

import entities.EstimationReport;

import java.awt.event.ActionListener;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class EstimationReportDesicion extends MasterGUI {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnConfirmReading;
	private MainGUI mainGui;
	private int processID;
	private EstimationReport EsReport;
	private getEstimationReport getEsRpck;
	private checkAllMemReadEstReport chkAllMemReadEsRep;
	private removeEsReport delEsRepPck;
	private UpdatePack updatePhs;
	private UpdatePack updRes;
	private UpdatePack waitStat;
	private JButton btnApprove;
	private JButton btnDisapprove;
	private JButton btnMoreData;
	private JPanel panelDecision;
	private UpdatePack upExecflag;
	

	public EstimationReportDesicion(MainGUI mainGui) {
		super(mainGui);
		this.mainGui=mainGui;
		
		System.out.println(MainGUI.process.getprocessID());
		this.processID=mainGui.process.getprocessID();
		System.out.println(this.processID);
		EsReport = new EstimationReport(processID);
		System.out.println("EsReport Ok");
		getEsRpck = new getEstimationReport(EsReport);
		System.out.println("EsReportPack Ok");
		mainGui.client.handleMessageFromClientUI(getEsRpck);
		System.out.println("SendToClient Ok");
		getEsRpck = ((getEstimationReport)(mainGui.client.getMessage()));
		System.out.println("getMessageOk");
		EsReport = getEsRpck.getReport();
		System.out.println(EsReport.getReportId()+" "+EsReport.getSystem()+" "+EsReport.getChangeDesc()+" "+EsReport.getLimitataions()+" "+EsReport.getHazards()+" "+EsReport.getEstTime());
		
		//Initiate The Process id to check if all the members has already read it's report
		chkAllMemReadEsRep = new checkAllMemReadEstReport(this.processID,0);
		
		//Initiate The Process id to delete the "old" EsReport - If Needed
		delEsRepPck = new removeEsReport(processID);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblEvaluationReport = new JLabel("Evaluation Report");
		lblEvaluationReport.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblEvaluationReport);
		
		//setBtnConfirmReading(btnConfirmReading);
		
		JLabel lblLocationOfChange = new JLabel("Location Of Change:");
		lblLocationOfChange.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLocationOfChange.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblLocationOfChange.setBounds(142, 86, 185, 23);
		getContentPane().add(lblLocationOfChange);
		
		JLabel lblResult = new JLabel("Result");
		lblResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblResult.setBounds(127, 150, 185, 23);
		getContentPane().add(lblResult);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDescription.setBounds(127, 238, 185, 23);
		getContentPane().add(lblDescription);
		
		JLabel lblLimitations = new JLabel("Limitations");
		lblLimitations.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLimitations.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblLimitations.setBounds(142, 336, 185, 23);
		getContentPane().add(lblLimitations);
		
		JLabel lblRisks = new JLabel("Risks");
		lblRisks.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRisks.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblRisks.setBounds(127, 407, 185, 23);
		getContentPane().add(lblRisks);
		
		JLabel lblExecutionTimeEstimation = new JLabel("Execution Time Estimation");
		lblExecutionTimeEstimation.setHorizontalAlignment(SwingConstants.RIGHT);
		lblExecutionTimeEstimation.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblExecutionTimeEstimation.setBounds(127, 490, 185, 23);
		getContentPane().add(lblExecutionTimeEstimation);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(350, 92, 204, 49);
		getContentPane().add(scrollPane);
		
		JTextArea textAreaLocation = new JTextArea(EsReport.getSystem());
		textAreaLocation.setEditable(false);
		scrollPane.setViewportView(textAreaLocation);
	
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(350, 244, 204, 75);
		getContentPane().add(scrollPane_1);
		
		JTextArea textAreaDesc = new JTextArea(EsReport.getChangeDesc());
		textAreaDesc.setEditable(false);
		scrollPane_1.setViewportView(textAreaDesc);
		
		JScrollPane scrollPane_0 = new JScrollPane();
		scrollPane_0.setBounds(350, 154, 204, 75);
		getContentPane().add(scrollPane_0);
		
		JTextArea textAreaRes = new JTextArea(EsReport.getResult());
		textAreaRes.setEditable(false);
		scrollPane_0.setViewportView(textAreaRes);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(350, 342, 204, 49);
		getContentPane().add(scrollPane_2);
		
		JTextArea textAreaLimit = new JTextArea(EsReport.getLimitataions());
		textAreaLimit.setEditable(false);
		scrollPane_2.setViewportView(textAreaLimit);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(350, 413, 204, 62);
		getContentPane().add(scrollPane_3);
		
		JTextArea textAreaRisks = new JTextArea(EsReport.getHazards());
		textAreaRisks.setEditable(false);
		scrollPane_3.setViewportView(textAreaRisks);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(350, 496, 204, 16);
		getContentPane().add(scrollPane_4);
		
		JTextArea textAreaTimeEst = new JTextArea(Integer.toString(EsReport.getEstTime()));
		textAreaTimeEst.setEditable(false);
		scrollPane_4.setViewportView(textAreaTimeEst);
		
		JLabel lblProcessIDTitle = new JLabel("Process ID:");
		lblProcessIDTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		lblProcessIDTitle.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblProcessIDTitle.setBounds(238, 51, 89, 24);
		getContentPane().add(lblProcessIDTitle);
		
		JLabel lblProcessIDNum = new JLabel(Integer.toString(this.processID));
		lblProcessIDNum.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblProcessIDNum.setBounds(350, 51, 131, 23);
		getContentPane().add(lblProcessIDNum);
		
		panelDecision = new JPanel();
		panelDecision.setBounds(211, 535, 441, 40);
		getContentPane().add(panelDecision);
		panelDecision.setLayout(null);
		
		
		
		setBtnApprove();
		setBtnDisapprove();
		setBtnMoreData();

		

		

		


	}


	/**
	 * 
	 * @return 1 if OK , 0 otherwise
	 */
public int checkAllMemRead(){
	System.out.println("before");
	mainGui.client.handleMessageFromClientUI(chkAllMemReadEsRep);
	System.out.println("mainGui.client.handleMessageFromClientUI(chkAllMemReadEsRep) Ok");
	chkAllMemReadEsRep = (checkAllMemReadEstReport)(mainGui.client.getMessage());
	System.out.println("chkAllMemReadEsRep.getBothRead()");
	return(chkAllMemReadEsRep.getBothRead());
}

/**
 * 
 * @return 1 if OK , 0 otherwise
 */
public int deleteEstimationReport(){
	int ans=0;
	mainGui.client.handleMessageFromClientUI(delEsRepPck);
	delEsRepPck = (removeEsReport)(mainGui.client.getMessage());
	ans=delEsRepPck.getDeleteOk();
	return(ans);
}

/**
 * 
 * @param phase - Phase number
 */
public void updatePhase3(int phase){
	updatePhs = new UpdatePack(MessagePack.updatePhase);
	updatePhs.setPhaseNum(phase);
	updatePhs.setProcId(mainGui.process.getprocessID());
	mainGui.client.handleMessageFromClientUI(updatePhs);
	upExecflag = new UpdatePack(MessagePack.updateExecTimeFlag);
	upExecflag.setProcId(mainGui.process.getprocessID());
	upExecflag.setResponOk(0);
	mainGui.client.handleMessageFromClientUI(upExecflag);
	
}

public void updateRes3(){
	updRes= new UpdatePack(MessagePack.updresponsible);
	updRes.setProcId(mainGui.process.getprocessID());
	updRes.setResponOk(0);
	mainGui.client.handleMessageFromClientUI(updRes);
	
}

/**
 * 
 * @param stat - Process status
 */
public void updateToAppStatus(String stat){
	waitStat = new UpdatePack(MessagePack.updateStatus);
	waitStat.setStatus(stat);
	waitStat.setProcId(mainGui.process.getprocessID());
	mainGui.client.handleMessageFromClientUI(waitStat);
}

public JButton getBtnApprove() {
	return btnApprove;
}

public void setBtnApprove() {
	//Approve Button + Listener
	btnApprove = new JButton("Approve");
	btnApprove.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			if(checkAllMemRead()==0)
			{
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Not All The Committee Memers Has Read The The Report!");	
			}
			else{
			//Decision Approve Treatment
			//Move Process To Phase 3
				updatePhase3(3);	
				updateRes3();
				updateToAppStatus("waiting For approval");
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Your Decision To Approve Got Accepted In System");
			btnApprove.setEnabled(false);
			btnDisapprove.setEnabled(false);
			btnMoreData.setEnabled(false);
			}
		}
	});
	btnApprove.setBounds(10, 11, 100, 23);
	panelDecision.add(btnApprove);
}

public JButton getBtnDisapprove() {
	return btnDisapprove;
}

public void setBtnDisapprove() {
	//Disapprove Button + Listener
	btnDisapprove = new JButton("Disapprove");
	btnDisapprove.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(checkAllMemRead()==0)
			{
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Not All The Committee Memers Has Read The The Report!");	
			}
			else{
			//Decision Disapprove Treatment
			//Move Process To Phase 5 - Change Status To Terminated
				updatePhase3(5);
				updateToAppStatus("Terminated");
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Your Decision To Disapprove Got Accepted In System");
			btnApprove.setEnabled(false);
			btnDisapprove.setEnabled(false);
			btnMoreData.setEnabled(false);
			}	
		}
	});
	btnDisapprove.setBounds(318, 11, 113, 23);
	panelDecision.add(btnDisapprove);
}

public JButton getBtnMoreData() {
	return btnMoreData;
}

public void setBtnMoreData() {
	//More Data Button + Listener
	btnMoreData = new JButton("Request For More Data");
	btnMoreData.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			if(checkAllMemRead()==0)
			{
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Not All The Committee Memers Has Read The The Report!");	
			}
			else{
			//Delete ESReport
			deleteEstimationReport();	
			//Move Process To Phase1
			updatePhase3(1);
			//Show Suitable Message
			JOptionPane.showMessageDialog(serverGUI.mainframe,"Your Decision To Request For More Data Got Accepted In System");
			btnApprove.setEnabled(false);
			btnDisapprove.setEnabled(false);
			btnMoreData.setEnabled(false);
			}	
		}
	});
	btnMoreData.setBounds(120, 11, 188, 23);
	panelDecision.add(btnMoreData);
}


	
	
}
